Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
Javascript one-dimentional array of object "like" Filter

I have one-dimentional array of object like this:

var data = [
{
  "empId": 63758,
  "empCode": "000003A",
  "empName": "Robert",
},
{
  "empId": 63759,
  "empCode": "000003B",
  "empName": "Paul John",
},
{
  "empId": 63760,
  "empCode": "000003C",
  "empName": "Chris John",
},
];

I want to filter the data just the way like when we use in sql query " like '%john%' ", and i wish it should be able to get non-case sensitive text as well. so it will output:

var data = [=
{
  "empId": 63759,
  "empCode": "000003B",
  "empName": "Paul John",
},
{
  "empId": 63760,
  "empCode": "000003C",
  "empName": "Chris John",
},
];

i already try this but the result is only accept full text parameter like "Chris John"

var filter = {
  "empName": "Chris John"
}
data = data.filter(function(item){
for(var key in filter){
    if(item[key] == undefined || item[key] != filter[key])
    return false
}
return true
});

Please help, thank you

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can simply achieve this by using String.indexOf() along with the Array.filter() method.

Live Demo :

var data = [
  {
    "empId": 63758,
    "empCode": "000003A",
    "empName": "Robert",
  },{
    "empId": 63759,
    "empCode": "000003B",
    "empName": "Paul John",
  },{
    "empId": 63760,
    "empCode": "000003C",
    "empName": "Chris John",
  }
];

const filter = {
  "empName": "john"
};

let res = data.filter(({ empName }) => empName.toLowerCase().indexOf(filter.empName) !== -1);

console.log(res);

about 4 years ago · Santiago Trujillo Denunciar

0

You can write a another util method to check the sub string values like below:

var data = [ { empId: 63758, empCode: "000003A", empName: "Robert", }, { empId: 63759, empCode: "000003B", empName: "Paul John", }, { empId: 63760, empCode: "000003C", empName: "Chris John", }, ];
var filter = {
  empName: "joh",
};

const checkLikeValue = (a, b) => {
  if(a === b) {
    return true
  } else if (typeof a === "string") {
    const substrings = a.split(" ");
    return substrings.some(str => b.toLowerCase().includes(str))
  }
  return false
}

data = data.filter(function (item) {
  for (var key in filter) {
    if (item[key] == undefined || !checkLikeValue(filter[key], item[key])) return false;
  }
  return true;
});

console.log(data);

about 4 years ago · Santiago Trujillo Denunciar

0

This can be achieved by using includes() as well as by indexOf() (as per the answer given by @Rohit Jindal).

Another approach would be to use includes.

var data = [
  {
    "empId": 63758,
    "empCode": "000003A",
    "empName": "Robert",
  },{
    "empId": 63759,
    "empCode": "000003B",
    "empName": "Paul John",
  },{
    "empId": 63760,
    "empCode": "000003C",
    "empName": "Chris John",
  }
];

const filter = {
  "empName": "john"
};

let res = data.filter(({ empName }) => empName.toLowerCase().includes(filter.empName));

console.log(res);

The only difference would be an additional check on the return value of indexOf() which is not needed when using includes(). Performance wise both clocks almost the same.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda